﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.DataBase.FrameWork;
using olierFrm.DataBase;
using DevExpress.Web.ASPxTabControl;
using olierFrm.WebFormDev.Plugin;

namespace olierFrm.WebFormDev.StdQuery
{
    public class StdQueryParam
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stdQueryParam"></param>
        /// <returns></returns>
        public static List<System.Web.UI.Control> StdQueryParamControls(List<WF_SmartQueryParams> stdQueryParam)
        {
            if (stdQueryParam == null) return null;
            List<System.Web.UI.Control> listControls = new List<System.Web.UI.Control>();
            var filterStdQueryParam = stdQueryParam.Where(p => p.ParamName.ToUpper() != "OPERATOR");//过滤系统参数
            foreach (var p in filterStdQueryParam)
            {
                List<WS_CommonItems> common = null;
                if (p.SQL != null && p.SQL.Trim().Length != 0)
                {
                    try
                    {
                        var dt = new SqlHelper().ExecuteQuery(p.SQL, null, "Common");
                        common = new List<WS_CommonItems>();
                        common.Add(new WS_CommonItems() { sComID = "", sComDesc = "" });
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string strValue = dt.Rows[i][0].ToString();
                            string value, text;
                            if (strValue.IndexOf(".") != -1)
                            {
                                var vs = strValue.Split('.');
                                value = vs[0];
                                text = vs[1];
                            }
                            else
                            {
                                value = text = strValue;
                            }
                            common.Add(new WS_CommonItems()
                            {
                                sComID = value,
                                sComDesc = strValue
                            });
                        }

                    }
                    catch { }
                }
                else if (p.PickList != null && p.PickList.Trim().Length > 2)
                {
                    common = new List<WS_CommonItems>();
                    var pls = p.PickList.Split(';').ToList();
                    foreach (var pl in pls)
                    {
                        string text, value;
                        if (pl.IndexOf(".") != -1)
                        {
                            var vs = pl.Split('.');
                            value = vs[0];
                            text = vs[1];
                        }
                        else
                        {
                            value = text = pl;
                        }
                        common.Add(new WS_CommonItems() { sComID = value, sComDesc = pl });
                    }
                }
                listControls.Add(
                HandelControl.CreateField(new ControlInfo()
                {
                    ParamName = p.ParamName,
                    ParamType = p.ParamType,
                    Common = common,
                    DisplayLabel = p.DisplayLabel,
                    DefaultValue = p.DefaultValue,
                    SQL = p.SQL
                }));

            }
            return listControls;
        }



        public ASPxPageControl StdQueryParamControls(List<WF_SmartQueryParams> stdQueryParam, string ControlID)
        {
            if (stdQueryParam == null) return null;
            var filterStdQueryParam = stdQueryParam.Where(p => p.ParamName.ToUpper() != "OPERATOR");//过滤系统参数
            var tabStdQueryParam = new ASPxPageControl();
            tabStdQueryParam.Style.Add("width", "100%");
            tabStdQueryParam.ID = ControlID;
            foreach (var p in filterStdQueryParam)
            {
                var tpGroup = tabStdQueryParam.TabPages.FindByText(p.Groups);
                if (tpGroup == null)
                {
                    tpGroup = new DevExpress.Web.ASPxTabControl.TabPage(p.Groups);
                    tabStdQueryParam.TabPages.Add(tpGroup);
                }
                //var panel = new DevExpress.Web.ASPxPanel.ASPxPanel();
                //panel.Style.Add("float", "left");
                //panel.Style.Add("margin-left", "10px");
                //panel.Style.Add("margin-bottom", "10px");
                //var control = HandelControl.GenControl(new ControlInfo() { ParamName = p.ParamName, ParamType = p.ParamType, PickList = p.PickList, DisplayLabel = p.DisplayLabel, DefaultValue = p.DefaultValue, SQL = p.SQL });
                //control.ID = p.ParamName;
                //var hgc = new HtmlGenericControl();
                //hgc.InnerHtml = p.DisplayLabel;

                //panel.Controls.Add(hgc);
                //panel.Controls.Add(new HtmlGenericControl("br"));
                //panel.Controls.Add(control);

                //处理下拉
                List<WS_CommonItems> common = null;
                if (p.PickList != null && p.PickList.Trim().Length > 2)
                {
                    common = new List<WS_CommonItems>();
                    var pls = p.PickList.Split(';').ToList();
                    foreach (var pl in pls)
                    {
                        var ci = pl.Split(';');
                        common.Add(new WS_CommonItems() { sComID = ci[0], sComDesc = ci[1] });
                    }
                }

                tpGroup.Controls.Add(HandelControl.CreateField(new ControlInfo()
                {
                    ParamName = p.ParamName,
                    ParamType = p.ParamType,
                    Common = common,
                    DisplayLabel = p.DisplayLabel,
                    DefaultValue = p.DefaultValue,
                    SQL = p.SQL
                }));
            }
            tabStdQueryParam.Style.Add("margin-bottom", "10px");
            return tabStdQueryParam;
        }

        public void StdQueryParamControls(List<WF_SmartQueryParams> stdQueryParam, ASPxPageControl pageControl)
        {
            if (stdQueryParam == null) return;

            //pageControl.TabPages.Clear();
            var filterStdQueryParam = stdQueryParam.Where(p => p.ParamName.ToUpper() != "OPERATOR" && p.ParamName.ToUpper() != "PARAMGROUP");//过滤系统参数

            pageControl.Style.Add("width", "100%");

            foreach (var p in filterStdQueryParam)
            {
                var tpGroup = pageControl.TabPages.FindByText(p.Groups);
                if (tpGroup == null)
                {
                    tpGroup = new TabPage(p.Groups);
                    pageControl.TabPages.Add(tpGroup);
                }
                //处理下拉
                List<WS_CommonItems> common = null;
                if (p.PickList != null && p.PickList.Trim().Length > 2)
                {
                    common = new List<WS_CommonItems>();
                    var pls = p.PickList.Split(';').ToList();
                    foreach (var pl in pls)
                    {
                        if (pl.IndexOf(".") == -1)
                        {
                            common.Add(new WS_CommonItems() { sComID = pl, sComDesc = pl });
                            continue;
                        }
                        var ci = pl.Split('.');
                        common.Add(new WS_CommonItems() { sComID = ci[0], sComDesc = ci[1] });
                    }
                }

                tpGroup.Controls.Add(HandelControl.CreateField(new ControlInfo()
                {
                    ParamName = p.ParamName,
                    ParamType = p.ParamType,
                    Common = common,
                    DisplayLabel = p.DisplayLabel,
                    DefaultValue = p.DefaultValue,
                    SQL = p.SQL
                }));
            }
            if (pageControl.TabPages.Count == 1)
                pageControl.TabPages[0].Text = "查询条件";
            pageControl.Style.Add("margin-bottom", "10px");
        }
    }
}
